package eu.dominicum.ft.pd.mao.validator;

import eu.dominicum.ft.pd.mao.MakeStageResultMAO;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * Custom validator to validate between the properties country and countryID.
 *
 * @author Harry Klerks
 * @since 2013M1
 */
public class NewOrExistingCountryValidator implements
        ConstraintValidator<NewOrExistingCountry, MakeStageResultMAO> {

    @Override
    public void initialize(final NewOrExistingCountry newOrExistingCountry) {

    }

    @Override
    public final boolean isValid(final MakeStageResultMAO makeStageResultMAO,
                                 final ConstraintValidatorContext constraintValidatorContext) {

        Long countryId = makeStageResultMAO.getMakeLocationMAO().getCountryId();
        Long locationId = makeStageResultMAO.getLocationId();
        String countryName = makeStageResultMAO.getMakeLocationMAO().getMakeCountryMAO().getName();
        String countryCode = makeStageResultMAO.getMakeLocationMAO().getMakeCountryMAO().getCountryCode();

        if (locationId != null) {
            return true;
        }

        return countryId != null || (countryName != null && countryCode != null);
    }


}
